<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParams.keyword"
          @input="changeValue"
          style="margin-bottom: 10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="deptTree"
          node-key="id"
          :data="depts"
          :props="defaultProps"
          :default-expand-all="true"
          highlight-current
          @current-change="selectNode"
        ></el-tree>
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button
            size="mini"
            type="primary"
            @click="$router.push('/employee/detail')"
            >添加员工</el-button
          >
          <el-button size="mini">excel导入</el-button>
          <el-button size="mini">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{ row }">
              <el-avatar
                v-if="row.staffPhoto"
                :src="row.staffPhoto"
                :size="30"
              ></el-avatar>
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名"></el-table-column>
          <el-table-column
            prop="mobile"
            label="手机号"
            sortable=""
          ></el-table-column>
          <el-table-column
            prop="workNumber"
            label="工号"
            sortable=""
          ></el-table-column>
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template v-slot="{ row }">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门"></el-table-column>
          <el-table-column
            prop="timeOfEntry"
            label="入职时间"
            sortable=""
          ></el-table-column>
          <el-table-column label="操作" width="280px">
            <template v-slot="{ row }">
              <el-button
                @click="$router.push(`/employee/detail/${row.id}`)"
                type="text"
                size="mini"
                >查看</el-button
              >
              <el-button @click="btnRole(row.id)" type="text" size="mini"
                >角色</el-button
              >
              <el-popconfirm
                title="确认要删除该行数据吗"
                @onConfirm="confirmDel(row.id)"
              >
                <el-button
                  slot="reference"
                  size="mini"
                  type="text"
                  style="margin-left: 10px"
                  >删除</el-button
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" type="flex" align="middle" justify="end">
          <el-pagination
            layout="total,prev,pager,next"
            :total="total"
            :current-page="queryParams.page"
            :page-size="queryParams.pagesize"
            @current-change="changePage"
          >
          </el-pagination>
        </el-row>
      </div>
    </div>
    <el-dialog :visible.sync="showRoleDialog" title="分配角色">
      <el-checkbox-group v-model="roleIds">
        <!-- 通过遍历数据，放置复选框，要存储的数据label:item.id, -->
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{
          item.name
        }}</el-checkbox>
      </el-checkbox-group>
      <!-- 使用弹窗本身带有的具名插槽footer -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="4">
          <el-button @click="btnRoleOk" type="primary" size="mini"
            >确定</el-button
          >
          <el-button @click="showRoleDialog=false" size="mini">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartment } from "@/api/department";
import {
  getEmployeeList,
  deltEmployee,
  getEnableRoleList,
  getEmployeeDetail,
  assignRole
} from "@/api/employee";
import { transListToTreeData } from "@/utils/index";
export default {
  name: "Employee",
  data() {
    return {
      /* 弹窗相关数据 */
      showRoleDialog: false,
      roleList: [], //存放角色权限数据
      roleIds: [], //存放角色权限的勾选项

      depts: [], //存放树形组件数据
      list: [], //存放员工数据
      defaultProps: {
        label: "name",
        children: "children",
      },
      //存储查询参数及分页查询数据
      queryParams: {
        departmentId: null, //要查询的树节点的id
        page: 1, //查询第一页
        pagesize: 10, //每页十条数据
        keyword: "", //关键字查询
      },
      /* 分页组件相关数据 */
      total: 0,
    };
  },
  created() {
    this.getDepartment();
  },
  methods: {
    /* 获取树节点数据 */
    async getDepartment() {
      let result = await getDepartment();
      this.depts = transListToTreeData(result, 0);
      //this.depts=transListToTreeData(await getDepartment(),0)
      /* 实现树组件节点默认高亮效果 */
      this.queryParams.departmentId = this.depts[0].id; //拿到第一个节点的id
      this.$nextTick(() => {
        this.$refs.deptTree.setCurrentKey(this.queryParams.departmentId);
      });
      this.getEmployeeList();
    },
    /* 点击树节点触发的事件 */
    selectNode(node) {
      //记录点击的树节点id，切换部门查询该节点下的员工数据
      this.queryParams.departmentId = node.id;
      this.queryParams.page = 1;
      this.getEmployeeList();
    },
    /* 获取员工数据 */
    async getEmployeeList() {
      let { rows, total } = await getEmployeeList(this.queryParams);
      this.list = rows;
      this.total = total;
    },
    /* 切换页码事件 */
    changePage(page) {
      this.queryParams.page = page;
      this.getEmployeeList();
    },
    /* 搜索员工姓名事件 */
    changeValue() {
      /* 简单的防抖处理 */
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.queryParams.page = 1;
        this.getEmployeeList();
      }, 300);
    },
    /* 气泡组件点击事件 */
    async confirmDel(id) {
      await deltEmployee(id);
      //当前页的数据条数等于1，并且当前页的页码数不是第一页时，向前翻页
      if (this.list.length === 1 && this.queryParams.page > 1) {
        this.queryParams.page--;
      }
      this.getEmployeeList();
      this.$message.success("删除成功");
    },
    /* 点击角色按钮 */
    async btnRole(id) {
      this.currentUserId = id;//找员工变量存储id
      this.roleList = await getEnableRoleList(); //获取所有的权限
      let { roleIds } = await getEmployeeDetail(id); //获取该角色拥有的权限
      this.roleIds = roleIds;
      this.showRoleDialog = true;
    },
    //点击确定新增角色权限
    async btnRoleOk(){
      await assignRole({id:this.currentUserId,roleIds:this.roleIds})
      this.$message.success('分配权限成功')
      this.showRoleDialog=false
    }
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin: 10px;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04c9be;
      font-size: 12px;
      display: inline-block;
    }
  }
}
</style>